import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '设备名称',			name: 'device_id',	type: 'select', loading:true, width:250 },
			{ label: '设备型号',			name: 'device_model' },
			{ label: '仪器项目名称',		name: 'device_pro_name' },
			{ label: '通道号',			name: 'item_name' },
		],
		// 提交表单列表
		formData : [
			{ label: '设备名称',			name: 'device_id',	type: 'select', required:true, half:true, onValue:({ ref, rows})=>{
				ref.setValue({ device_number: rows ? rows.device_number : null })
			} },
			{ label: '设备编号',			name: 'device_number', 	half:true, disabled:true },
			{ label: '设备项目名称',		name: 'device_pro_name', required:true, half:true, idStr:'code' },
			{ label: '通道号',			name: 'pass_num', 		half:true,	required:true, },
			{ label: '描述', 			name: 'desc_info', full:true,         type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		$cache.then( async f => {
			// 设备名称
			$fn.setRows([ forms, formData ], 'device_id', await f.getDivCache() )
			//
			this.setState({ forms })
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'de-pass-info/index')
	
	// 表单
	cols = [
		{ title: '设备名称',			dataIndex: 'device_name', 		width:200 },
		{ title: '设备编号', 		dataIndex: 'device_number', 	width:150 },
		{ title: '设备型号', 		dataIndex: 'device_model', 		width:150 },
		{ title: '仪器项目名称', 	dataIndex: 'device_pro_name', 		width:150 },
		{ title: '描述', 			dataIndex: 'desc_info', 		width:150, },
		{ title: '通道号', 			dataIndex: 'pass_num', 			width:150, sorter:true },
		TableControls.controls.call(this)
	]
	
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'de-pass-info/info', { uuid }),				// 编辑
			this.onDel([ uuid ]),															// 删除
		]
	}
	// 操作按钮
	controls = () => [
		Controls.add.call(this,{label:'新增通道'}),					// 添加
		this.onDel(),												// 删除
		Controls.imports.call(this, 'de-pass-info/import', {op_key: 'DePassInfoNumExcel', reconfirm: true}),			// 文件导入
		// 下载导入模板
		Controls.importTemplate.call(this),
		// 实验室管理-仪器通道管理增加导出  耿琰 20220630
		Controls.exports.call(this, 'de-pass-info/index',{ export:1 }),		// 导出
	]
	// 删除
	onDel = uuid => Delete.call(this, 'de-pass-info/del', { uuid, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='仪器通道管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{sort:true, isInit:window.isInit}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ title:'仪器通道',  api: 'de-pass-info/add', editApi:'de-pass-info/edit' }) }
			</>
		)
	}
}

export default Index